package com.swss.g.sys.action;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.swss.g.base.BaseAction;
import com.swss.g.sys.bean.CurrentPeriod;
import com.swss.g.sys.bean.GBygl;
import com.swss.g.sys.bean.GFunc;
import com.swss.g.sys.service.CurrentPeriodService;
import com.swss.g.sys.service.FuncService;
import com.swss.g.sys.service.ByglService;
import com.swss.g.sys.service.PrintService;
import com.swss.g.sys.service.SysService;
import com.swss.g.sys.service.impl.SysServiceImpl;
import com.swss.util.Pagination;

@SuppressWarnings("serial")
public class SysAction extends BaseAction {

	private String gbNo;

	private String root;

	private String gbKeyword;

	private Boolean updateFlag;
	/**
	 * 
	 */
	private static final long serialVersionUID = 2379150879673960259L;

	/***************************************************************************
	 * param
	 */
	private String url;
	private List<?> timeList;
	private GFunc systates; // 系统阶段 0.建账 1.维护账
	private GFunc formExp;
	private List<GFunc> funcList;
	private List<GBygl> byglList;
	private String barCode;
	private String accDoc;
	private String searchType;
	private String fileNames[];
	private String keyword;// 查询关键字 searchNation查询国别码使用
	private String callback;
	private String deptNo;
	/***************************************************************************
	 * bean
	 */
	private CurrentPeriod currentPeriod;
	private Pagination pagination;
	private GFunc gfunc;

	/**
	 * list
	 */
	private List<Integer> ids = new ArrayList<Integer>();

	private List<String> newSFs = new ArrayList<String>();

	private List<String> deVals = new ArrayList<String>();

	/***************************************************************************
	 * service
	 */

	private SysService sysService;
	private CurrentPeriodService currentPeriodService;
	private FuncService funcService;
	private ByglService gbyglService;
	private PrintService printService;

	static Logger log = Logger.getLogger(SysAction.class);

	public String gbtree() {
		try {
			this.print(this.sysService.findGBCodeJSONByNo(root));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	public String gbSearch() {
		try {
			print(this.sysService.findGBJSONByKeyword(this.gbKeyword));
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
		return null;
	}

	public String findAllTime() {

		try {
			if (pagination == null) {
				pagination = new Pagination();
			}

			pagination = this.currentPeriodService.findAll(pagination);
			// timeList = pagination.getResult();
			pagination.setUrl("/swss" + this.getRequest().getServletPath());

			System.out.println(pagination.getResult().size());
			System.out.println(pagination);
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}

	/**
	 * 默认查询系统参数设置 param systates,formExp //系统状态、登记表单样式
	 * 
	 * @return
	 */
	public String findAllFunc() {

		try {
			if(this.msg!=null){
				this.msg=new String(msg.getBytes("iso8859-1"), "UTF-8");
			}
			this.systates = this.funcService.findSystateName("sysstates");
			this.formExp = this.funcService.findFormExp("formExp");
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}

	/**
	 * 更新系统参数设置
	 * 
	 * @return
	 */
	public String updateFunc() {
		try {
			funcList = new ArrayList<GFunc>();
			funcList.add(this.systates);
			funcList.add(this.formExp);
			this.updateFlag = this.funcService.updateFuncs(funcList);
			this.code=0;
			this.msg = URLEncoder.encode("参数设置成功", "utf-8");
			return "findAllFunc";
		} catch (Exception ex) {
			ex.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 查询备用管理
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String findAllBygl() {
		try {
			if(this.msg!=null){
				this.msg=new String(msg.getBytes("iso8859-1"), "UTF-8");
			}
			byglList = this.gbyglService.findAllStandField();
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}

	/**
	 * 修改备用字段
	 * 
	 * @return
	 */
	public String modifySF() {
		try {
			/**
			 * DOTO: 页面遍历列表对象时，action里设置get、set方法 取属性原名称 可获得 [*,*,*] 格式数组
			 */
			this.gbyglService.updateStandFields(ids, newSFs, deVals);
			this.code=0;
			this.msg = URLEncoder.encode("备用设置成功", "utf-8");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 获取打印相关
	 * 
	 * @return
	 */
	public String findPrintWay() {
		try {
			if(this.msg!=null){
				this.msg=new String(msg.getBytes("iso8859-1"), "UTF-8");
			}
			barCode = this.printService.findUsedPrint(1);
			accDoc = this.printService.findUsedPrint(2);
			return "setPrint";
		} catch (Exception e) {
			e.printStackTrace();
			return "INPUT";
		}
	}

	/**
	 * 修改 打印设置
	 * 
	 * @return
	 */
	public String updatePrint() {
		try {
			this.printService.updatePrint(barCode, accDoc);
			this.code=0;
			this.msg = URLEncoder.encode("表单设置成功", "utf-8");
			return "setPrint";
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 查询现有备份.DAT 文件
	 * 
	 * @return
	 */
	public String findDBFiles() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		if (request.getParameter("searchType") == null) {
			this.searchType = (String) session.getAttribute("searchType");
		} else {
			this.searchType = request.getParameter("searchType");
		}
		session.setAttribute("searchType", searchType);
		try {
			this.fileNames = this.sysService.dbFileNames(ServletActionContext
					.getServletContext().getRealPath("/html/backup"));
			if (this.searchType.equals("backup")) {
				return "backup";
			}
			if (this.searchType.equals("restore")) {
				return "restore";
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 开始备份
	 * 
	 * @return
	 */
	public String backUpDB() {
		try {
			this.sysService.backUpDB(ServletActionContext.getServletContext()
					.getRealPath("/html/backup"));
			return "findDBFiles";
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 删除备份
	 * 
	 * @return
	 */
	public String deleteDB() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String fileName = request.getParameter("fileName");
		try {
			this.sysService.deleteDB(fileName, ServletActionContext
					.getServletContext().getRealPath("/html/backup"));
			return "findDBFiles";
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 还原数据
	 * 
	 * @return
	 */
	public String restoreDB() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String fileName = request.getParameter("fileName");
		try {
			this.sysService.restoreDB(ServletActionContext.getServletContext()
					.getRealPath("/html/backup"), fileName);
			return "findDBFiles";
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String searchNation() {
		try {

			this.print(this.sysService.searchNation(keyword));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("sysAction 查询国别码错误");
			e.printStackTrace();
		}
		return null;
	}

	public String searchStorage() {
		try {
			this.print(this.sysService.searchStorage(keyword, deptNo));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("sysAction 查询存放地点错误");
			e.printStackTrace();
		}
		return null;
	}

	public SysService getSysService() {
		return sysService;
	}

	public void setSysService(SysService sysService) {
		this.sysService = sysService;
	}

	public String getGbNo() {
		return gbNo;
	}

	public void setGbNo(String gbNo) {
		this.gbNo = gbNo;
	}

	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public String getGbKeyword() {
		return gbKeyword;
	}

	public void setGbKeyword(String gbKeyword) {
		this.gbKeyword = gbKeyword;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List<?> getTimeList() {
		return timeList;
	}

	public void setTimeList(List<?> timeList) {
		this.timeList = timeList;
	}

	public CurrentPeriod getCurrentPeriod() {
		return currentPeriod;
	}

	public void setCurrentPeriod(CurrentPeriod currentPeriod) {
		this.currentPeriod = currentPeriod;
	}

	public CurrentPeriodService getCurrentPeriodService() {
		return currentPeriodService;
	}

	public void setCurrentPeriodService(
			CurrentPeriodService currentPeriodService) {
		this.currentPeriodService = currentPeriodService;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public FuncService getFuncService() {
		return funcService;
	}

	public void setFuncService(FuncService funcService) {
		this.funcService = funcService;
	}

	public GFunc getGfunc() {
		return gfunc;
	}

	public void setGfunc(GFunc gfunc) {
		this.gfunc = gfunc;
	}

	public GFunc getSystates() {
		return systates;
	}

	public void setSystates(GFunc systates) {
		this.systates = systates;
	}

	public GFunc getFormExp() {
		return formExp;
	}

	public void setFormExp(GFunc formExp) {
		this.formExp = formExp;
	}

	public List<GFunc> getFuncList() {
		return funcList;
	}

	public void setFuncList(List<GFunc> funcList) {
		this.funcList = funcList;
	}

	public Boolean getUpdateFlag() {
		return updateFlag;
	}

	public void setUpdateFlag(Boolean updateFlag) {
		this.updateFlag = updateFlag;
	}

	public List<GBygl> getByglList() {
		return byglList;
	}

	public void setByglList(List<GBygl> byglList) {
		this.byglList = byglList;
	}

	public ByglService getGbyglService() {
		return gbyglService;
	}

	public void setGbyglService(ByglService gbyglService) {
		this.gbyglService = gbyglService;
	}

	public List<Integer> getId() {
		return ids;
	}

	public void setId(List<Integer> ids) {
		this.ids = ids;
	}

	public List<String> getNewSF() {
		return newSFs;
	}

	public void setNewSF(List<String> newSFs) {
		this.newSFs = newSFs;
	}

	public List<String> getDeVal() {
		return deVals;
	}

	public void setDeVal(List<String> deVals) {
		this.deVals = deVals;
	}

	public String getBarCode() {
		return barCode;
	}

	public void setBarCode(String barCode) {
		this.barCode = barCode;
	}

	public String getAccDoc() {
		return accDoc;
	}

	public void setAccDoc(String accDoc) {
		this.accDoc = accDoc;
	}

	public PrintService getPrintService() {
		return printService;
	}

	public void setPrintService(PrintService printService) {
		this.printService = printService;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public String[] getFileNames() {
		return fileNames;
	}

	public void setFileNames(String[] fileNames) {
		this.fileNames = fileNames;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getCallback() {
		return callback;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}

	public String getDeptNo() {
		return deptNo;
	}

	public void setDeptNo(String deptNo) {
		this.deptNo = deptNo;
	}

}
